package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.beans.TradeProvinceOrderAmount;
import com.atguigu.gmall.publisher.service.TradeStatsService;
import com.atguigu.gmall.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2023/11/22
 * 交易域统计Controller
 * @RestController:将当前类对象的创建交给spring容器,如果类中的方法返回的是String类型，直接进行返回
 * @Controller    :将当前类对象的创建交给spring容器,如果类中的方法返回的是String类型，会进行页面的跳转
 *                  如果不想进行页面的跳转，需要在方法的上面加@ResponseBody注解
 *         @RestController = @Controller + @ResponseBody
 * @RequestMapping():拦截客户端的请求，将请求交给标注的方法的进行处理
 * @RequestParam():接收请求中携带的参数，如果没有传参，可以给指定的默认值
 */
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            //如果没有传递日期参数，将当天日期作为查询日期
            date = DateFormatUtil.now();
        }

        BigDecimal gmv = tradeStatsService.getGMV(date);

        String json = "{\"status\": 0,\"data\": "+gmv+"}";
        return json;
    }

    /*@RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceAmountList.size(); i++) {
            TradeProvinceOrderAmount orderAmount = provinceAmountList.get(i);
            jsonB.append("{\"name\": \""+orderAmount.getProvinceName()+"\",\"value\": "+orderAmount.getOrderAmount()+"}");
            if(i < provinceAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }*/
    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }

        Map resMap = new HashMap();
        resMap.put("status",0);
        Map dataMap = new HashMap();
        List dataList = new ArrayList();
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);
        for (TradeProvinceOrderAmount orderAmount : provinceAmountList) {
            Map map = new HashMap();
            map.put("name",orderAmount.getProvinceName());
            map.put("value",orderAmount.getOrderAmount());
            dataList.add(map);
        }

        dataMap.put("mapData",dataList);
        dataMap.put("valueName","交易额");
        resMap.put("data",dataMap);
        return resMap;
    }
}
